<template>
	<view v-show="goodInfo.goods_name" class="goodsPage">
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="500" class="infoImg" circular>
			<swiper-item v-for="(item,index) in goodInfo.pics" :key="index">
				<image :src="item.pics_big"  @click="preview(index)"></image>
			</swiper-item>
		</swiper>
		<view class="price">￥{{goodInfo.goods_price}}</view>
		<view class="describe">
			<view class="describeLeft">{{goodInfo.goods_name}}</view>
			<view class="describeRight">
				<uni-icons class="describeRightIcon" type="star" size="20"></uni-icons>
				<text>收藏</text>
			</view>
		</view>
		<view class="yf">
			快递：免运费
		</view>
		<rich-text :nodes="goodInfo.goods_introduce"></rich-text>
		<!-- fill 控制右侧按钮的样式 -->
		  <!-- options 左侧按钮的配置项 -->
		  <!-- buttonGroup 右侧按钮的配置项 -->
		  <!-- click 左侧按钮的点击事件处理函数 -->
		  <!-- buttonClick 右侧按钮的点击事件处理函数 -->
		<uni-goods-nav class="nav" :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
	</view>
	
</template>

<script>
	import  { mapState, mapMutations, mapGetters } from 'vuex'
	export default {
		data() {
			return {
				goods_id: '',
				goodInfo: {},
				options: [{
							icon: 'shop',
							text: '店铺',
						}, {
							icon: 'cart',
							text: '购物车',
							info: 0
						}],
					    buttonGroup: [{
					      text: '加入购物车',
					      backgroundColor: '#ff0000',
					      color: '#fff'
					    },
					    {
					      text: '立即购买',
					      backgroundColor: '#ffa200',
					      color: '#fff'
					    }
					    ]
			}
		},
		watch:{
		// 	total(newVal) {
		// 		// 2. 通过数组的 find() 方法，找到购物车按钮的配置对象
		// 	  const findResult = this.options.find((x) => x.text === '购物车')
		
		// 	  if (findResult) {
		// 		// 3. 动态为购物车按钮的 info 属性赋值
		// 		findResult.info = newVal
		// 	  }
		// 	}
			
			total: {
				handler(newVal) {
					// 2. 通过数组的 find() 方法，找到购物车按钮的配置对象
				  const findResult = this.options.find((x) => x.text === '购物车')
			
				  if (findResult) {
					// 3. 动态为购物车按钮的 info 属性赋值
					findResult.info = newVal
				  }
				},
				immediate: true
			}
		},
		computed: {
			// 调用 mapState 方法，把 m_cart 模块中的 cart 数组映射到当前页面中，作为计算属性来使用
			    // ...mapState('模块的名称', ['要映射的数据名称1', '要映射的数据名称2'])
			// ...mapState('m_cart', ['cart'])
			...mapGetters('m_cart', ['total'])
		},
		methods: {
			async getGoodDetail(goods_id) {
				const {data} = await uni.$http.get('/api/public/v1/goods/detail',{goods_id})
				if (data.meta.status !== 200) return uni.$showMsg()
				
				this.goodInfo = data.message
			},
			...mapMutations('m_cart', ['ADDTOCART']),
			preview(i) {
				uni.previewImage({
					// 预览时，默认展示图片的索引
					current: i,
					// 所有图片url地址的数组
					urls: this.goodInfo.pics.map(x => x.pics_big)
				})
			},
			onClick(e) {
				if (e.content.text === '购物车') {
					uni.switchTab({
						url:"../../pages/cart/cart"
					})
				}
			},
			buttonClick(e) {
				if (e.content.text === '加入购物车') {
					// 每个商品的信息对象，都包含如下 6 个属性：
					// { goods_id, goods_name, goods_price, goods_count, goods_small_logo, goods_state }
					const info = {
						goods_id: this.goodInfo.goods_id,
						goods_name: this.goodInfo.goods_name,
						goods_price: this.goodInfo.goods_price,
						goods_count: 1,
						goods_small_logo: this.goodInfo.goods_small_logo,
						goods_state: false
					}
					this.ADDTOCART(info)
				}
			}
		},
		onLoad(options) {
			this.goods_id = options.goods_id
			this.getGoodDetail(this.goods_id)
		}
	}
</script>

<style lang="scss">
.goodsPage {
	padding-bottom: 50px;
	.infoImg {
		height: 750rpx !important;
		image {
			width: 100%;
			height: 100%;
		}
	}
	.price {
		color: #c00;
		padding: 0 20rpx;
		font-size: 20px;
		margin-top: 20rpx;
	}
	.yf {
		color: gray;
		padding: 0 20rpx;
		font-size: 14px;
		margin: 20rpx 0;
	}
	.describe {
		padding: 0 20rpx;
		font-size: 13px;
		height: 36px;
		display: flex;
		margin-top: 20rpx;
		.describeLeft {
			flex: 1;
			max-height: 36px;	
			border-right: 1px solid #ccc;
			overflow: hidden;
			-webkit-line-clamp: 2;  /*要显示的行数*/
			text-overflow: ellipsis;
			padding-right: 20rpx;
		}
		.describeRight {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			width: 110rpx;
		}
	}
	.nav {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
	}
}
</style>
